// Enqueue script for parent theme stylesheet function example_enqueue_styles() { wp_enqueue_style('parent-theme', get_template_directory_uri() . '/style.css'); wp_enqueue_style('child-theme', get_stylesheet_directory_uri() . '/style.css', array('parent-theme')); } add_action('wp_enqueue_scripts', 'example_enqueue_styles'); // Register AJAX actions for saving chat messages add_action('wp_ajax_save_chat_message', 'save_chat_message_to_db'); add_action('wp_ajax_nopriv_save_chat_message', 'save_chat_message_to_db'); function save_chat_message_to_db() { global $wpdb; // Get data from the AJAX request $name = sanitize_text_field($_POST['name']); $message = sanitize_textarea_field($_POST['message']); $image = $_FILES['image'] ?? null; // Adjusted to handle file upload $image_url = ''; // Process image upload if there is one if (!empty($image) && $image['error'] === UPLOAD_ERR_OK) { $upload_dir = wp_upload_dir(); // Get WordPress upload directory $image_path = $upload_dir['path'] . '/' . basename($image['name']); if (move_uploaded_file($image['tmp_name'], $image_path)) { // Save the URL instead of the image binary data $image_url = $upload_dir['url'] . '/' . basename($image['name']); } } // Save data into the custom table $table_name = $wpdb->prefix . 'chat_messages'; $result = $wpdb->insert( $table_name, [ 'name' => $name, 'message' => $message, 'image' => $image_url, // Store image URL 'timestamp' => current_time('mysql'), ] ); // Send a response if ($result) { wp_send_json_success('Message saved successfully'); } else { wp_send_json_error('Failed to save message'); } wp_die(); // Always terminate AJAX functions properly } // Register AJAX actions for retrieving chat messages add_action('wp_ajax_get_chat_messages', 'get_chat_messages'); add_action('wp_ajax_nopriv_get_chat_messages', 'get_chat_messages'); function get_chat_messages() { global $wpdb; $table_name = $wpdb->prefix . 'chat_messages'; // Fetch messages from the database, ordered by timestamp $messages = $wpdb->get_results("SELECT * FROM $table_name ORDER BY timestamp ASC"); // Send the messages as a JSON response wp_send_json_success($messages); wp_die(); } // Create the database table for storing chat messages function create_chat_messages_table() { global $wpdb; $table_name = $wpdb->prefix . 'chat_messages'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, message text NOT NULL, image text DEFAULT NULL, // Store image as a URL timestamp datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } add_action('after_setup_theme', 'create_chat_messages_table'); {"code":"rest_no_route","message":"No route was found matching the URL and request method.","data":{"status":404}}